//
//  LDSSliderViewController.m
//  TestCategory
//
//  Created by kai_leedarson on 2017/11/6.
//Copyright © 2017年 maple_leedarson. All rights reserved.
//

#import "LDSSliderViewController.h"
#import "LDSSliderViewControllerViewModel.h"
#import "LDSSliderCollectionViewModel.h"
#import "LDSSliderHeadViewController.h"
#import "UIResponder+Router.h"
#import "NYWaterWaveView.h"

@interface LDSSliderViewController ()
@property(nonatomic, strong) LDSSliderViewControllerViewModel *viewControllerViewModel;//必须子类化
@property(nonatomic, strong) UICollectionView *collectionView;
@property(nonatomic, strong) LDSSliderCollectionViewModel *collectionViewModel;//选择性子类化
@property(nonatomic, strong) UICollectionViewFlowLayout *collectionLayout;
@property(nonatomic, strong) NSMutableArray *dataSource;//cell 数据源
@property(nonatomic, strong) LDSSliderHeadViewController *sliderHeadViewController;//
@end

@implementation LDSSliderViewController

#pragma mark - lazy init
- (LDSSliderHeadViewController *)sliderHeadViewController {
    if (_sliderHeadViewController == nil) {
        _sliderHeadViewController = [[LDSSliderHeadViewController alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
        [_sliderHeadViewController createData:@[@"YYY1",@"YYY2",@"YYY3",@"YYY4",@"YYY5"]];
    }
    return _sliderHeadViewController;
}


- (LDSSliderViewControllerViewModel *)viewControllerViewModel {
    if (_viewControllerViewModel == nil) {
        _viewControllerViewModel = [[LDSSliderViewControllerViewModel alloc] init];
        _viewControllerViewModel.delegate = (id<LDSViewControllerViewModelDelegate>) self;
    }
    return _viewControllerViewModel;
}

- (LDSSliderCollectionViewModel *)collectionViewModel {
    if (_collectionViewModel == nil) {
        _collectionViewModel = [[LDSSliderCollectionViewModel alloc] init];
        _collectionViewModel.delegate = (id<LDSCollectionViewModelDelegate>)self;
        //接管collectionView
        [_collectionViewModel handleWithCollection:self.collectionView];
        
    }
    return _collectionViewModel;
}

- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height - 40 - 64)
                                             collectionViewLayout:self.collectionLayout];
        //_collectionView.backgroundColor = RGB(235, 241, 241) ;
        _collectionView.backgroundColor = [UIColor yellowColor];
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

- (UICollectionViewLayout *)collectionLayout {
    if (_collectionLayout == nil) {
        _collectionLayout = [[UICollectionViewFlowLayout alloc] init];
        [_collectionViewModel handleWithCollectionLayout:_collectionLayout];
        CGFloat photoSizeW = self.view.frame.size.width;
        CGFloat photoSizeH = self.view.frame.size.height - 40 - 64;
        [_collectionLayout setItemSize:(CGSize){photoSizeW, photoSizeH}];
        [_collectionLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        [_collectionLayout setMinimumLineSpacing:0.0f];
    }
    return _collectionLayout;
}

- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (NSString *)barItemTitle {
    return @"";
}

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.viewControllerViewModel loadFetchData];
    [self.view addSubview:self.sliderHeadViewController];
    NYWaterWaveView *waterWaveView = [[NYWaterWaveView alloc]initWithFrame:CGRectMake(0,100, 200, 200)];
    //    waterWaveView.backgroundColor = [UIColor redColor];
    [self.view addSubview:waterWaveView];
}

#pragma mark - LDSCollectionViewModelDelegate
- (void)lds_collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

}

//- (UIEdgeInsets)lds_collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
//    return UIEdgeInsetsMake(14, 14,14, 14);
//}

#pragma mark - SecondVCViewModelDelegate
- (void)lds_didReceiveDataWithDataSource:(NSArray *)dataSource andSectionDataSource:(NSArray *)sectionDataSource {
    self.dataSource = [dataSource mutableCopy];
    [self.collectionViewModel handleWithDataSource:self.dataSource];
    [self.collectionView reloadData];
}

- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo {
    if ([eventName isEqualToString:@"headCellSelected"]) {
        NSIndexPath *indexpath = userInfo[@"indexPath"];
        if (indexpath.row < [self.dataSource[indexpath.section] count]) {
            [self.collectionView scrollToItemAtIndexPath:userInfo[@"indexPath"] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        }
    } else if ([eventName isEqualToString:@"scrollEnd"]) {
        UIScrollView *scrollView = userInfo[@"scrollView"];
        int index = scrollView.contentOffset.y / scrollView.frame.size.width;
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
//        [self.sliderHeadViewController.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    }
}
@end
